<?php
/**
 * Gpsclient.class.php
 * 
 * 贵漂客户端
 * 
 * @author lsl <470663151@qq.com> 
 * @copyright Copyright (c) 2015 lsl8966 Software Studio
 * @link http://www.cnblogs.com/lsl8966
 * @version $Id: Gpsclient.class.php 1.0 2012-01-18 22:48:01Z lsl8966 $
 * @package core
 * @since 1.0
 */
class Gpsclient extends  \gps\lib\Base{
    private static $app_id;
    private static $app_secret;
    private static $ver = "1.0";
    private static $client_type = "3"; //1 ios 2 android 3 web
    private static $gps_url = "http://192.168.200.208:8080/kwinbon";


    public function __construct(){

    }

    /**
     *
     */
    public function __destruct(){
    }

    public static function factory($app_id,$app_secret,$client_type = 3){
        self::$app_id = $app_id;
        self::$app_secret = $app_secret;
        self::$client_type = $client_type;
        return new self();
    }

    public static function setGpsServerUrl($url){
        self::$gps_url = $url;
    }

    public  function requestBase($params){
        $params = array_merge(array('ver'=>self::$ver),$params);
        $params = array_merge(array('api_sign'=>self::$app_secret),$params);
        $params = array_merge(array('api_key'=>self::$app_id),$params);
        $params = array_merge(array('client_type'=>self::$client_type),$params);
        return $params;
    }

    public static function request($params){
        $params = array_merge(self::requestBase($params),$params);
        if($_GET['is_debug'] == 1){
            echo self::$gps_url."?json=".json_encode($params);
        }
        return \gps\lib\curl::postRequest("http://192.168.200.208:8080/kwinbon/?json=".self::encode_json($params));
    }

    /**
     * 处理编码问题
     * @param $str
     * @return string
     */
    static function encode_json($str) {
        return urldecode(json_encode(self::url_encode($str)));
    }

    /**
     *
     */
    static function url_encode($str) {
        if(is_array($str)) {
            foreach($str as $key=>$value) {
                $str[urlencode($key)] = self::url_encode($value);
            }
        } else {
            $str = urlencode($str);
        }

        return $str;
    }
}
